﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;

namespace Pixysoft.Framework.Logs.Core
{
    class LogFrameCollection : ILogFrameCollection
    {
        IXmlNodesCollection collection = null;

        public LogFrameCollection(IXmlNodesCollection collection)
        {
            this.collection = collection;
        }

        public ILogFrame this[int index]
        {
            get
            {
                IXmlNode node = collection[index];
                if (node == null)
                    return null;
                return new LogFrame(node);
            }
        }

        public int Count
        {
            get
            {
                return collection.Count;
            }
        }

        public void Add(ILogFrame frame)
        {
            if (frame == null)
                return;

            this.Add(frame.Dll, frame.Class, frame.Method, frame.File, frame.Path, frame.Line.ToString(), frame.Column.ToString());
        }

        public ILogFrame Add(IXmlNode node)
        {
            if (node == null)
                return null;
            collection.Add(node);
            return new LogFrame(node);
        }

        public ILogFrame Add(string dll, string _class, string method, string file, string path, string line, string column)
        {
            if (string.IsNullOrEmpty(dll))
                return null;

            if (string.IsNullOrEmpty(_class))
                return null;

            if (!string.IsNullOrEmpty(path))
                path = StringFilter.RegularPath(path);

            IXmlNode node = collection.Add(StringHelper.default_element_frame);
            node.Properties[StringHelper.default_attribute_dll] = dll;
            node.Properties[StringHelper.default_attribute_class] = _class;
            if (!string.IsNullOrEmpty(method))
                node.Properties[StringHelper.default_attribute_method] = method;
            if (!string.IsNullOrEmpty(file))
                node.Properties[StringHelper.default_attribute_file] = file;
            if (!string.IsNullOrEmpty(path))
                node.Properties[StringHelper.default_attribute_path] = path;
            if (!string.IsNullOrEmpty(line))
                node.Properties[StringHelper.default_attribute_line] = line;
            if (!string.IsNullOrEmpty(column))
                node.Properties[StringHelper.default_attribute_column] = column;

            return new LogFrame(node);
        }


        #region IEnumerable<ILogFrame> 成员

        public IEnumerator<ILogFrame> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<ILogFrame> 成员

        public ILogFrame Current
        {
            get
            {
                return new LogFrame(collection.Current);
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            collection.Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return new LogFrame(collection.Current);
            }
        }

        public bool MoveNext()
        {
            return collection.MoveNext();
        }

        public void Reset()
        {
            collection.Reset();
        }

        #endregion
    }
}
